/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package business;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author
 */
public class Emailer
{

    public void sendEmail(
             String aToEmailAddr,
            String aSubject, String aBody)
    {
        //Here, no Authenticator argument is used (it is null).
        //Authenticators are used to prompt the user for user
        //name and password.
        Session session = Session.getDefaultInstance(fMailServerConfig, null);
        MimeMessage message = new MimeMessage(session);
        try
        {
            //the "from" address may be set in code, or set in the
            //config file under "mail.from" ; here, the latter style is used
            //message.setFrom( new InternetAddress(aFromEmailAddr) );
            message.addRecipient(
                    Message.RecipientType.TO, new InternetAddress(aToEmailAddr));
            message.setSubject(aSubject);
            message.setText(aBody);
            Transport.send(message);
        }
        catch (MessagingException ex)
        {
            System.err.println("Cannot send email. " + ex);
        }
    }

    /**
     * Allows the config to be refreshed at runtime, instead of
     * requiring a restart.
     */
    public static void refreshConfig()
    {
        fMailServerConfig.clear();
        fetchConfig();
    }
    // PRIVATE //
    private static Properties fMailServerConfig = new Properties();

    static
    {
        fetchConfig();
    }

    /**
     * Open a specific text file containing mail server
     * parameters, and populate a corresponding Properties object.
     */
    private static void fetchConfig()
    {
        InputStream input = null;
        try
        {
            //If possible, one should try to avoid hard-coding a path in this
            //manner; in a web application, one should place such a file in
            //WEB-INF, and access it using ServletContext.getResourceAsStream.
            //Another alternative is Class.getResourceAsStream.
            //This file contains the javax.mail config properties mentioned above.
            input = new FileInputStream("C:\\Temp\\MyMailServer.txt");
            fMailServerConfig.load(input);
        }
        catch (IOException ex)
        {
            System.err.println("Cannot open and load mail server properties file.");
        }
        finally
        {
            try
            {
                if (input != null)
                {
                    input.close();
                }
            }
            catch (IOException ex)
            {
                System.err.println("Cannot close mail server properties file.");
            }
        }
    }
}